package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.bo.fee.CostItem;
import com.neu.his.bo.fee.FeePayRec;
import com.neu.his.bo.fee.FeeRec;
import com.neu.his.bo.fee.FeeSend;
import com.neu.his.core.Result;
import com.neu.his.entity.DiagRecords;
import com.neu.his.entity.DrugDisturb;
import com.neu.his.entity.NonDrugDisturb;
import com.neu.his.entity.Patient;
import com.neu.his.mapper.*;
import com.neu.his.service.registFee02.FeeService;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FeeServiceImpl implements FeeService {
    @Autowired
    private DiagRecordMapper diagRecordMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DrugDisturbMapper drugDisturbMapper;
    @Autowired
    private NonDrugDisturbMapper nonDrugDisturbMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private NonDrugMapper nonDrugMapper;

    public void updateDiagCount(Long diagID, BigDecimal count){
        DiagRecords diagRecords = diagRecordMapper.selectById(diagID);

        BigDecimal tmp = new BigDecimal(diagRecords.getRealCost().toString());
        tmp = tmp.add(count);
        UpdateWrapper<DiagRecords> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.lambda().eq(DiagRecords::getId, diagRecords.getId())
                .set(DiagRecords::getRealCost, tmp);
        diagRecordMapper.update(null, updateWrapper1);
    }

    @Override
    public Result getCostItem(FeeRec feeRec) {
        FeeSend feeSend = new FeeSend();
        List<CostItem> list = new ArrayList<>();
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Patient::getIdNumber, feeRec.getPatientId());
        List<Patient> patients = patientMapper.selectList(queryWrapper);
        if (patients.size() == 0) {
            return Result.fail("该患者不存在！");
        }
        feeSend.setPatient(patients.get(0));
        //获取该患者所有的诊断记录
        List<DiagRecords> diagRecords = diagRecordMapper.selectList(
                new QueryWrapper<DiagRecords>().lambda()
                        .eq(DiagRecords::getPatientId, patients.get(0).getId())
        );
        //对于每个诊断记录，获取其关联的药品及非药品分配
        for (DiagRecords diagRecords1 : diagRecords) {
            //获取药品中未付款的项目
            QueryWrapper<DrugDisturb> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(DrugDisturb::getDiagId, diagRecords1.getId());
            List<DrugDisturb> drugDisturbs = drugDisturbMapper.selectList(queryWrapper1);
            for (DrugDisturb drugDisturb : drugDisturbs) {
                if (drugDisturb.getRemoveMark() == 0) {
                    if (drugDisturb.getRealCount().intValue() == 0) {
                        CostItem costItem = new CostItem();
                        costItem.setCostType("药品");
                        costItem.setCostId(drugDisturb.getId());
                        costItem.setCostName(drugMapper.selectById(drugDisturb.getDrugId()).getDrugName());
                        costItem.setTotalCost(drugDisturb.getTotalCount());
                        costItem.setCreateTime(drugDisturb.getCreateDate());
                        list.add(costItem);
                    }
                }
            }
            //获取非药品中未付款的项目
            List<NonDrugDisturb> nonDrugDisturbs = nonDrugDisturbMapper.selectList(
                    new QueryWrapper<NonDrugDisturb>().lambda()
                            .eq(NonDrugDisturb::getDiagId, diagRecords1.getId())
            );
            for (NonDrugDisturb nonDrugDisturb : nonDrugDisturbs) {
                if (nonDrugDisturb.isPaid() == false) {
                    CostItem costItem = new CostItem();
                    costItem.setCreateTime(nonDrugDisturb.getCreateDate());
                    costItem.setTotalCost(nonDrugDisturb.getTotalCount());
                    costItem.setCostName(nonDrugMapper.selectById(nonDrugDisturb.getNonDrugId()).getNonDrugName());
                    costItem.setCostType("非药品");
                    costItem.setCostId(nonDrugDisturb.getId());
                    list.add(costItem);
                }
            }

        }
        PageList pageList = new PageList(list,feeRec.getCurPage(),feeRec.getPageSize());
        feeSend.setPatient(patients.get(0));
        feeSend.setData(pageList);
        return Result.success(feeSend);
    }

    @Override
    public Result payItem(FeePayRec feePayRec) {
        for (CostItem costItem : feePayRec.getData()) {
            Long diagId = new Long(0);
            if (costItem.getCostType().equals("药品")) {
                UpdateWrapper<DrugDisturb> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(DrugDisturb::getId, costItem.getCostId())
                        .set(DrugDisturb::getRealCount, costItem.getTotalCost());
                drugDisturbMapper.update(null, updateWrapper);

                DrugDisturb drugDisturb = drugDisturbMapper.selectById(costItem.getCostId());
                updateDiagCount(drugDisturb.getDiagId(),costItem.getTotalCost());
            } else {
                UpdateWrapper<NonDrugDisturb> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", costItem.getCostId()).set("is_paid", true);
                nonDrugDisturbMapper.update(null, updateWrapper);

                NonDrugDisturb nonDrugDisturb = nonDrugDisturbMapper.selectById(costItem.getCostId());
                updateDiagCount(nonDrugDisturb.getDiagId(),costItem.getTotalCost());
            }

        }
        return Result.success();
    }
}
